/* MIT License
 *
 * Copyright (c) 2012 Reuben Smith, Miami University
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.droidlike.vsig;


import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;


/**
 * ProfileSelectDialog.
 * 
 * @author smithre5
 */
public class ProfileSelectDialog extends javax.swing.JDialog
{
    /**
     * Creates new form ProfileSelectDialog
     */
    public ProfileSelectDialog(java.awt.Frame parent, boolean modal)
    {
        super(parent, modal);
        initComponents();
    }


    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jLabel1 = new javax.swing.JLabel();
        profileCombo = new javax.swing.JComboBox();
        selectProfileBtn = new javax.swing.JButton();
        createProfileBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setUndecorated(true);
        setResizable(false);

        jLabel1.setText("Profile:");

        selectProfileBtn.setText("Select");
        selectProfileBtn.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                selectProfileBtnActionPerformed(evt);
            }
        });

        createProfileBtn.setText("Create");
        createProfileBtn.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                createProfileBtnActionPerformed(evt);
            }
        });

        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                cancelBtnActionPerformed(evt);
            }
        });

        String[] profileNames = Profile.getProfileNames();
        String[] profileOptions = new String[profileNames.length + 1];

        profileOptions[0] = Profile.DEFAULT_NAME;
        for (int i = profileOptions.length; --i > 0;)
        {
            profileOptions[i] = profileNames[i - 1];
        }

        profileCombo.setModel(new DefaultComboBoxModel<String>(profileOptions));
        if (profileCombo.getModel().getSize() == 0)
        {
            selectProfileBtn.setEnabled(false);
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(profileCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cancelBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 194, Short.MAX_VALUE)
                        .addComponent(createProfileBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectProfileBtn)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(profileCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectProfileBtn)
                    .addComponent(createProfileBtn)
                    .addComponent(cancelBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelBtnActionPerformed
    {//GEN-HEADEREND:event_cancelBtnActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void selectProfileBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectProfileBtnActionPerformed
    {//GEN-HEADEREND:event_selectProfileBtnActionPerformed
        if (((String)profileCombo.getSelectedItem()).equals(Profile.DEFAULT_NAME)) {
            VSIG.setProfile(null);
        }
        else {
            VSIG.setProfile((String)profileCombo.getSelectedItem());
        }
        
        this.dispose();
    }//GEN-LAST:event_selectProfileBtnActionPerformed

    private void createProfileBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_createProfileBtnActionPerformed
    {//GEN-HEADEREND:event_createProfileBtnActionPerformed
        this.setVisible(false);
        
        ProfileCreateDialog createDlg = new ProfileCreateDialog((JFrame)this.getParent(), true);
        createDlg.setLocationRelativeTo(this.getParent());
        createDlg.setVisible(true);
        
        this.dispose();
    }//GEN-LAST:event_createProfileBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JButton createProfileBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox profileCombo;
    private javax.swing.JButton selectProfileBtn;
    // End of variables declaration//GEN-END:variables

}

